package com.yc.jpyy.jcanjuan.controller;


import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.common.utils.manydatasource.HandlerDataSource;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.jcanjuan.service.JCAnJuanService;
import com.yc.jpyy.utils.Tools;

@Controller
@RequestMapping(value = "/JCAnJuan")
@Scope("prototype")
public class JCAnJuanController extends BaseController{
	private static Logger logger = LoggerFactory.getLogger(JCAnJuanController.class);

	@Autowired
	private JCAnJuanService jCAnJuanService;
	
	/**
	 * 新增
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/addJCAnJuan", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String addJCAnJuan(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			String info = request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = jCAnJuanService.addJCAnJuan(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("addJCAnJuan" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "添加失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
		
	}
	
	/**
	 * 根据ID查询
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/selectJCAnJuanById", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String selectJCAnJuanById(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			String info= request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = jCAnJuanService.selectJCAnJuanById(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("selectJCAnJuanById" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "查询失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
		
	}
	
	/**
	 * 根据JC_ID查询
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/selectJCAnJuanByJcid", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String selectJCAnJuanByJcid(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			String info= request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = jCAnJuanService.selectJCAnJuanByJcid(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("selectJCAnJuanByJcid" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "查询失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
		
	}
	
	/**
	 * 更新
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/updateJCAnJuan", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String updateJCAnJuan(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			String info= request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = jCAnJuanService.updateJCAnJuan(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("updateJCAnJuan" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "更新失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
		
	}
	
	/**
	 * 获取稽查案卷的流水号
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getJcajNum", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String getJcajNum(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			String info= request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(info);
			HandlerDataSource.setDataSource("A1");
			resultMap = jCAnJuanService.getJcajNum(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("getJcajNum" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "获取失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
		
	}
	
	
	/**
	 * 撤回稽查案卷
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/revertJcajNum", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String revertJcajNum(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			String info= request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(info);
			HandlerDataSource.setDataSource("A1");
			resultMap = jCAnJuanService.revertJcajNum(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("revertJcajNum" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "撤回失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
		
	}
	
}
